﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Shmup_Project
{

    public class AnimatedSprite :WorldObject
    {
        public int currentFrame;
        Rectangle drawFrame;
        double ElapsedTime;

        public struct SpriteAnimation
        {
            public int totalFrames;
            public int frameHeight, frameWidth;
            public double frameLength;
            public Vector2 drawPosition;
            public bool blooping;
            public int rows, columns;
        }

        public SpriteAnimation currentAnimation;
        public AnimatedSprite(Game1 gameClass, int xPosition, int yPosition, int Height, int Width)
            : base(gameClass, xPosition, yPosition, Height, Width)
        {
        }

        public override void Update(GameTime gameTime)
        {
            UpdateSpriteFrame(gameTime);
            base.Update(gameTime);
        }
        public void UpdateSpriteFrame(GameTime gametime)
        {
            ElapsedTime += gametime.ElapsedGameTime.TotalMilliseconds;
            if (ElapsedTime >= currentAnimation.frameLength)
            {
                currentFrame++;
                ElapsedTime = 0;
                if (currentFrame >= currentAnimation.totalFrames)
                {
                    if (currentAnimation.blooping)
                        currentFrame = 0;
                    else
                        currentFrame = currentAnimation.totalFrames - 1;
                }
                UpdateDrawRectangle();
            }
            
        }
        public override void UpdateRectanglePosition()
        {
            base.UpdateRectanglePosition();
        }
        public void UpdateDrawRectangle()
        {
            int currentRow = currentFrame / currentAnimation.columns;
            int currntCollum = currentFrame % currentAnimation.columns;

            drawFrame.X = currntCollum * currentAnimation.frameWidth;
            drawFrame.Y = currentRow * currentAnimation.frameHeight;
            drawFrame.Width = currentAnimation.frameWidth;
            drawFrame.Height = currentAnimation.frameHeight;
        }
        public override void DrawSprite(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, boundingBox,drawFrame,Color.White);   
        }
    }

}
